package com.wondertek.mist.plugins;

import org.apache.cordova.CordovaArgs;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import com.wondertek.mist.gallery.WebViewer;



public class WebViewPlugin extends CordovaPlugin{
	public static final String MIST_WEBURL_EXTRA = "webURL";
	public static final String MIST_MESSAGE_EXTRA = "message";
	public static final String MIST_TYPE_EXTRA = "type";
	public static final String MIST_PLACEHOLDER_EXTRA = " placeholder";
//	public static final String MIST_OKBUTTONTITLE_EXTRA = "okButtonTitle";
	
	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
		
				JSONObject message = (JSONObject) args.getJSONObject(0);
                 
				Log.i("JSONObject", message.toString());
				
				initWebView(message);
				
				Log.i("initWeb", "yes");
				return true;
			
	}

	private void initWebView(final JSONObject message) {
		// TODO Auto-generated method stub
		final Activity cvdActivity = this.cordova.getActivity();

		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Intent intent = new Intent(cvdActivity,WebViewer.class);
					intent.putExtra(MIST_TYPE_EXTRA, message.getString("type"))
					.putExtra(MIST_MESSAGE_EXTRA, message.getString("message"))
					.putExtra(MIST_WEBURL_EXTRA, message.getString("webURL"))
					.putExtra(MIST_PLACEHOLDER_EXTRA, message.getString("placeholder"));
					
					Log.i("putExtra",message.getString("webURL") );

					cvdActivity.startActivity(intent);

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
			
		cvdActivity.runOnUiThread(runnable);
		
		
		

	}



}
